﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace SemestralniPraceY35VAN
{
    class Tip
    {
        /// <summary>
        /// Properties of the tip.
        /// </summary>
        private Hashtable properties;

        /// <summary>
        /// 
        /// </summary>
        public string ID { get { if (properties.ContainsKey("id")) return (string)properties["id"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public string Text { get { if (properties.ContainsKey("text")) return (string)properties["text"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public int Distance { get { if (properties.ContainsKey("distance")) return Int16.Parse((string)properties["distance"]); else return 0; } }

        /// <summary>
        /// 
        /// </summary>
        public string Created { get { if (properties.ContainsKey("created")) return (string)properties["created"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public User User { get { if (properties.ContainsKey("user")) return (User)properties["user"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public Venue Venue { get { if (properties.ContainsKey("venue")) return (Venue)properties["venue"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public string Status { get { if (properties.ContainsKey("status")) return (string)properties["status"]; else return null; } }

        /// <summary>
        /// Constructs an object representing a tip.
        /// </summary>
        /// <param name="tip">XML node with information about the tip</param>
        public Tip(XmlNode tip)
        {
            properties = new Hashtable();

            if (tip.Name == "tip")
            {
                for (int i = 0; i < tip.ChildNodes.Count; i++)
                {
                    switch (tip.ChildNodes[i].Name)
                    {
                        case "user":
                            properties.Add(tip.ChildNodes[i].Name, new User(tip.ChildNodes[i]));
                            break;
                        case "venue":
                            properties.Add(tip.ChildNodes[i].Name, new Venue(tip.ChildNodes[i]));
                            break;
                        default:
                            properties.Add(tip.ChildNodes[i].Name, tip.ChildNodes[i].InnerText);
                            break;
                    }
                }
            }
            else
            {
                throw new IOException("Not Tip!");
            }
        }
    }
}
